<template>
  <div id="detail">
    <detail-nav-bar class="detail-nav" @titleClick="titleClick" ref="nav"/>

    <scroll class="content" :probe-type="3" ref="scroll" @scroll="contentScroll">
      <detail-swiper :topImages="topImages"/>
      <detail-base-info :goods="goods"/>
      <detail-shop-info :shop="shop"/>
      <detail-goods-info :detail-info="detailInfo" @detailImageLoad="detailImageLoad"/>
      <detail-param-info ref="params" :param-info="paramInfo"/>
      <detail-comment-info ref="comment" :comment-info="commentInfo"/>
      <goods-list ref="recommend" :goods="recommends"/>
    </scroll>

    <detail-bottom-bar @addToCart="addToCart"/>
    <back-top @click.native="backClick" v-show="isShowBackTop"/>
<!--    <toast :message="message" :show="show"/>-->

  </div>
</template>

<script>

// 自身的组件
import DetailNavBar from "./childComps/DetailNavBar";
import DetailSwiper from "./childComps/DetailSwiper";
import DetailBaseInfo from "./childComps/DetailBaseInfo";
import DetailShopInfo from "./childComps/DetailShopInfo";
import DetailGoodsInfo from "./childComps/DetailGoodsInfo";
import DetailParamInfo from "./childComps/DetailParamInfo";
import DetailCommentInfo from "./childComps/DetailCommentInfo";
import DetailBottomBar from "./childComps/DetailBottomBar";

//公用的组件
import GoodsList from "components/content/goods/GoodsList";
import Scroll from "components/common/scroll/Scroll";


import {getDetail, Goods, Shop, GoodsParam, getRecommend} from "network/detail";
import {backTopMixin} from 'common/mixin';

import {mapActions} from 'vuex'
// import Toast from "components/common/tosat/Toast";


export default {
  name: "Detail",
  components: {
    // Toast,
    DetailBottomBar,
    DetailCommentInfo,
    DetailParamInfo,
    DetailShopInfo,
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    Scroll,
    DetailGoodsInfo,
    GoodsList
  },
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommends: [],
      themeTopYs: [],
      currenIndex: 0,
     /* message: '',
      show: false*/


    }
  },
  mixins: [backTopMixin],
  created() {
    //1.保存传入的iid
    this.iid = this.$route.params.iid

    //2.根据iid请求详情数据
    getDetail(this.iid).then(res => {
      // console.log(res)
      const data = res.result
      //1.获取顶部的图片轮播数据
      this.topImages = data.itemInfo.topImages

      //2.获取商品信息
      this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services)

      //3.获取商家店铺信息 (数据整合)
      this.shop = new Shop(data.shopInfo)
      // this.shop = data.shopInfo  // 也可以直接获取数据

      //4.保存商品的详情数据
      this.detailInfo = data.detailInfo;

      //5.获取参数信息
      this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule)

      //6.获取评论信息
      if (data.rate.cRate) {
        this.commentInfo = data.rate.list[0]
      }

      /* //第一次获取，值不对
       //值不对的原因 ： this.$refs.params.$el 压根没有渲染
       this.themeTopYs = []
       this.themeTopYs.push(0)
       this.themeTopYs.push(this.$refs.params.$el.offsetTop)
       this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
       this.themeTopYs.push(this.$refs.recommend.$el.offsetTop)

       this.$nextTick(() => {
         //第二次获取： 值不对
         // 图片没有计算在内
         //根据最新的数据， 对应的DOM已经被渲染出来
         //但是图片依然没有加载完（目前获取的offsetTop是不包含图片的）
         this.themeTopYs.push(0)
         this.themeTopYs.push(this.$refs.params.$el.offsetTop)
         this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
         this.themeTopYs.push(this.$refs.recommend.$el.offsetTop)

         console.log(this.themeTopYs)
       })*/

    });

    //3.请求推荐数据
    getRecommend().then(res => {
      this.recommends = res.data.list
    })

  },
  methods: {
    ...mapActions(['addCart']),
    detailImageLoad() {
      this.$refs.scroll.refresh()

      setTimeout(() => {
        this.themeTopYs = []
        this.themeTopYs.push(0)
        this.themeTopYs.push(this.$refs.params.$el.offsetTop - 44)
        this.themeTopYs.push(this.$refs.comment.$el.offsetTop - 44)
        this.themeTopYs.push(this.$refs.recommend.$el.offsetTop - 44)
        /*  this.themeTopYs.push(this.$refs.params.$el.offsetTop)
          this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
          this.themeTopYs.push(this.$refs.recommend.$el.offsetTop)*/
        this.themeTopYs.push(Number.MAX_VALUE)

        console.log(this.themeTopYs)
      }, 1500)

    },
    titleClick(index) {
      // console.log(index)
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 800)
    },
    contentScroll(position) {
      //1.获取Y值
      const positionY = -position.y

      //2.positionY和主题中的值进行对比
      let length = this.themeTopYs.length
      for (let i = 0; i < length - 1; i++) {
        if (this.currenIndex !== i && (positionY >= this.themeTopYs[i] && positionY < this.themeTopYs[i + 1])) {
          this.currenIndex = i
          this.$refs.nav.currentIndex = this.currenIndex
        }
        //3.判断BackTop 是否显示
        this.isShowBackTop = -position.y > 1000

      }
    },
    addToCart() {
      //1.获取购物车需要展示的信息
      const product = {}
      product.image = this.topImages[0];
      product.title = this.goods.title;
      product.desc = this.goods.desc;
      product.price = this.goods.realPrice;
      product.iid = this.iid

      //2.讲商品添加到购物车
      // this.$store.cartList.push(product)
      // this.$store.commit('addCart', product)

    /*  this.$store.dispatch('addCart', product).then(res => {
        console.log(res)
      }).catch(error => error)*/

      this.addCart(product).then(res => {

        //封装为插件
        this.$toast.show(res, 1500)

        //单独使用
        /*this.show =true
        this.message = res
        console.log(res)

        setTimeout(()=> {
          this.show = false
          this.message = ''
        }, 1500)*/
      })


    }
  },

   /*updated() {
     this.themeTopYs = []
     this.themeTopYs.push(0)
    /!* this.themeTopYs.push(this.$refs.params.$el.offsetTop - 44)
     this.themeTopYs.push(this.$refs.comment.$el.offsetTop - 44)
     this.themeTopYs.push(this.$refs.recommend.$el.offsetTop - 44)*!/
       this.themeTopYs.push(this.$refs.params.$el.offsetTop)
       this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
       this.themeTopYs.push(this.$refs.recommend.$el.offsetTop)
     this.themeTopYs.push(Number.MAX_VALUE)

     console.log(this.themeTopYs)
   }*/


}
</script>

<style scoped>

#detail {
  position: relative;
  z-index: 29;
  background-color: #fff;
  height: 100vh;
}

.detail-nav {
  position: relative;
  z-index: 99;
  background-color: #fff;
}

.content {
  height: calc(100% - 44px);
}
</style>
